﻿package framework.custom;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateCode {
	
	private static int DEFAULT_LENGTH = 4;
	
	public static String RANDOM_CODE_NAME = "RandomCodeName";
	
	private static char[] MIXED_CHAR_ARRAY = new char[] { '2', '3', '4', '5',
			'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
			'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V',
			'W', 'X', 'Y', 'Z' };
	private static char[] INTEGER_CHAE_ARRAY = new char[] { '0', '1', '2', '3',
			'4', '5', '6', '7', '8', '9' };
	/*
	 * 取得随机颜色
	 */
	public static Color getRandColor(int frontColor, int backgroundColor){
		Random random = new Random();
		if(frontColor > 255) frontColor = 255;
		if(backgroundColor > 255) backgroundColor = 255;
		
		int r = frontColor + random.nextInt(backgroundColor - frontColor);
		int g = frontColor + random.nextInt(backgroundColor - frontColor);
		int b = frontColor + random.nextInt(backgroundColor - frontColor);
		
		return new Color(r, g, b);
	}
	
	/**
	 * 取随机字符串
	 */
	public static String getRandString(char[] chars, int length)
	{
		if(chars == null || chars.length == 0) return CreateCode.getRandString(DEFAULT_LENGTH);
		if(length <= 0) return CreateCode.getRandString(DEFAULT_LENGTH);
		
		String sRand = "";
		Random random = new Random();
		for(int i = 0; i < length; i++){
			sRand = sRand + chars[Math.abs(random.nextInt(chars.length))];
		}
		return sRand;		
	}
	public static String getRandString(int length)
	{
		return getRandString(INTEGER_CHAE_ARRAY, length);
	}
	public static String getRandString()
	{
		return getRandString(DEFAULT_LENGTH);
	}
	
	/**
	 * 生成随机验证图片
	 */
	public static RenderedImage getValidateImage(int width, int height, String code){
//		int frontColor = 150, backgroundColor = 250;
				
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g= image.getGraphics();
//		g.setColor(CreateCode.getRandColor(frontColor, backgroundColor));
		g.setColor(new Color(200, 200, 200));
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("Time New Roman", Font.ITALIC | Font.BOLD, 18));
		
		//产生5条干扰线
//		g.setColor(CreateCode.getRandColor(160, 200));
		g.setColor(Color.BLACK);
		Random random = new Random();
//		for(int i = 0; i < 5; i++){
//			int x = random.nextInt(24);
//			int y = random.nextInt(16);
//			int xl = random.nextInt(120);
//			int yl = random.nextInt(20);
//			g.drawLine(x, y, x + xl, yl);
//		}
        //产生50个干扰点
		for(int i = 0; i < 50; i++){
			int x = random.nextInt(width);
			int y = random.nextInt(height);
//			g.setColor(CreateCode.getRandColor(160, 200));
//			g.setColor(Color.RED);
			g.drawLine(x, y, x + random.nextInt(2), y + random.nextInt(2));
		}

		String sRand = code;//CreateCode.getRandString(MIXED_CHAR_ARRAY, 4);
		for(int i = 0; i < 4; i++){
			String tp = String.valueOf(sRand.charAt(i));
//			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
//			g.drawString(tp, 13 * (i * 2) + 6, 20 - (random.nextInt(8)) );
			g.drawString(tp, 13 * (i * 2) + 6, 16);
		}
		
		g.dispose();
		return image;
	}
	
	public static RenderedImage getValidateImage(String code)
	{
		return getValidateImage(120, 20, code);
	}
		
	/**
	 * 显示图片
	 * @param request
	 * @param response
	 */
	public static void displayValidateImage(HttpServletRequest request, HttpServletResponse response){
		try{
			ServletOutputStream oStream = response.getOutputStream();
			String code = getRandString(MIXED_CHAR_ARRAY, 4);
			request.getSession().setAttribute(RANDOM_CODE_NAME, code);
			RenderedImage image = CreateCode.getValidateImage(code);
			ImageIO.write(image, "JPEG", oStream);
		}
		catch(Exception e){
			System.err.println("生成图片中发生错误！");
		}
	}
}
